home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / VirtualLight / VLight1.3win32.exe / VibSDK / Samples / sample1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-07  |  3.5 KB  |  132 lines

  1. /*
  2.  * VirtuaLight's binary .VIB format API, sample 1
  3.  * Written by Stephane Marty, 09/10/2001
  4.  *
  5.  * This sample program writes a binary VIB file
  6.  * describing a simple procedural shell object.
  7.  */
  8.  
  9. #include "..\vlBinDef.h"
  10.  
  11. #define MAXEPS     (1.0e6)
  12.  
  13. static int level;
  14. static double A, B, G, coeff, rfactor;
  15.  
  16. void main(void)
  17. {
  18.     unsigned long pri=0;
  19.     viCAMERA        *cam;
  20.     viGENERAL        *gen;
  21.     viPOINT_LIGHT    *pl;
  22.     viFILE            *vib;
  23.     viSPHERE        sphere;
  24.     viDISK            disk;
  25.     viVECTOR        Gv, Pv, vec;
  26.     double r,angle;
  27.     int i, steps;
  28.  
  29.     viSetVector(&Gv, -MAXEPS, -MAXEPS, -MAXEPS);
  30.     viSetVector(&Pv, +MAXEPS, +MAXEPS, +MAXEPS);
  31.     level = 2;
  32.     A = 0.0;
  33.     B = -1.5; 
  34.     G = 1.0;
  35.     coeff = 0.15;
  36.     rfactor = 1.0;
  37.  
  38.     // Open a new VIB file
  39.     vib = viNewBinaryVIB("sample1.vib");
  40.  
  41.     // Create and declare the procedural object named "shell"
  42.     viDeclareNewObject("shell", vib);
  43.     steps = (int)(180.0 * pow(2.0, (double)level));
  44.     for (i=-steps*2/3; i<=steps/3; ++i)
  45.     {
  46.         angle = 3.0 * 6.0 * M_PI * (double)i / (double)steps;
  47.         r = rfactor * exp(coeff * angle);
  48.         viSetDbl(sphere.center.x, r * sin(angle));
  49.         viSetDbl(sphere.center.y, r * cos(angle));
  50.         if (A > 0.0)
  51.             viSetDbl(sphere.center.z, A * angle);
  52.         else
  53.             viSetDbl(sphere.center.z, B * r);
  54.         viSetDbl(sphere.radius, r/G);
  55.         // this shell is a simple set of spheres
  56.         viDumpSphere(&sphere, vib);
  57.         if (sphere.center.x > Gv.x) Gv.x = sphere.center.x;
  58.         if (sphere.center.y > Gv.y) Gv.y = sphere.center.y;
  59.         if (sphere.center.z > Gv.z) Gv.z = sphere.center.z;
  60.         if (sphere.center.x < Pv.x) Pv.x = sphere.center.x;
  61.         if (sphere.center.y < Pv.y) Pv.y = sphere.center.y;
  62.         if (sphere.center.z < Pv.z) Pv.z = sphere.center.z;
  63.         pri++;
  64.     }
  65.     viEndObjectDeclaration(vib);
  66.  
  67.     // Add the camera (low adaptive antialiasing enabled)
  68.     cam = viNewCamera();
  69.     viSetInt(cam->Format.X, 384);
  70.     viSetInt(cam->Format.Y, 288);
  71.     viSetVector(&cam->Location,
  72.         (Gv.x+Pv.x)/2.0,
  73.         ((Gv.y+Pv.y)/2.0+Gv.y)*1.1,
  74.         Gv.z*5.0+10);
  75.     viSetVector(&cam->LookAt,
  76.         (Pv.x+Gv.x)/2.0,
  77.         (Pv.y+Gv.y)/2.0,
  78.         (Pv.z+Gv.z)/2.0);
  79.     viSetVector(&cam->UpAxis, 0, 0, 1);
  80.     viSetDbl(cam->FieldOfView, 60);
  81.     viSetInt(cam->Antialiasing, 1);
  82.     viDumpCamera(cam, vib);
  83.  
  84.     // Set the background color
  85.     gen = viNewGeneral();
  86.     viSetColor(&gen->Background, 0.4,0.4,0.5);
  87.     viDumpGeneral(gen, vib);
  88.  
  89.     // Add a key pointlight
  90.     pl = viNewPointLight();
  91.     viSetColor(&pl->Intensity, 0.7,0.7,0.7);
  92.     viSetVector(&pl->Position,
  93.         cam->Location.x*8,
  94.         cam->Location.y*3,
  95.         25);
  96.     viDumpPointLight(pl, vib);
  97.  
  98.     // Add a fill pointlight (no shadow)
  99.     pl = viNewPointLight();
  100.     viSetColor(&pl->Intensity, 0.5,0.5,0.5);
  101.     viSetVector(&pl->Position,
  102.         -cam->Location.x*2,
  103.         -cam->Location.y,
  104.         100);
  105.     viSetByte(pl->LightingAttributes, LIGHT_ATTRIB_SPECULAR);
  106.     viDumpPointLight(pl, vib);
  107.  
  108.     // Invoke the object (scaled down by 0.5)
  109.     viCallObject("shell", vib);
  110.     viObjectShaderName("shell_shader", vib);
  111.     viDumpScale(viSetVector(&vec, 0.5, 0.5, 0.5), vib);
  112.     viEndObjectCall(vib);
  113.  
  114.     // Add a simple disk for the floor (detached from the object)
  115.     viPrimitive(vib);
  116.     viSetVector(&disk.center, 0, 0, Pv.z);
  117.     viSetVector(&disk.normal, 0, 0, 1);
  118.     viSetDbl(disk.radius, 200);
  119.     viDumpDisk(&disk, vib);
  120.     viPrimitiveShaderName("ground", vib);
  121.     viEndPrimitive(vib);
  122.  
  123.     // Close the VIB file
  124.     viCloseBinaryVIB(vib);
  125.  
  126.     // Deallocate memory used
  127.     free(cam);
  128.     free(gen);
  129.     free(pl);
  130.  
  131.     fprintf(stderr, "\n%lu spheres dumped.\n", pri);
  132. }